using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Killboard.Models;
using KillParser;

namespace Killboard.Controllers
{
    public class KillController : Controller
    {
        //
        // GET: /Kill/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult KillDisplay(int id)
        {
            Models.KillRepository repo = new Killboard.Models.KillRepository();

            Models.ekbKill kill =  repo.GetKill(id);

            return View(kill);
        }

        public ActionResult KillDisplay2(int id)
        {
            return KillDisplay(id);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult PostMail(FormCollection fc)
        {
            KillMail km = new KillMail();
            string kmtext = fc["KMText"];
            string postingPass = fc["postingPass"];

            KillRepository repo = new KillRepository();
            if(!repo.CheckPassword(postingPass))
            {
                ViewData["postingPass"] = postingPass;
                ViewData["KMText"] = kmtext;
                ViewData.ModelState.AddModelError("postingPass", "Invalid posting password.");
                return View();
            }
    

            km.Parse(kmtext);

            int killNumber = km.SaveKill();

            //return View("Index", km);
            return RedirectToAction("KillDisplay2", "Kill", new { id = killNumber });


        }

        public ActionResult PostMail()
        {
            return View();
        }

        public ActionResult ShowKillsByType(string shipType)
        {
            KillRepository repo = new KillRepository();
            ViewData["shipType"] = shipType;

            var kills = repo.GetKillsAndLossesByType(shipType);


            return View(kills);
        }

        public ActionResult CorpKillLoss(bool showKills)
        {

            KillRepository repo = new KillRepository();
            IEnumerable<Models.KillStats> killsByShip = repo.SumKillsByShip();
            ViewData["killSummary"] = killsByShip;

            
            if (showKills)
            {
                return View(repo.GetAllKills());
            }
            else
            {
                return View(repo.GetAllLosses());
            }
        }

        public ActionResult PersonalStats(int pilotID)
        {
            KillRepository repo = new KillRepository();

            var kills = repo.GetKillsAndLossesForPilot(pilotID);
            
            IEnumerable<Models.KillStats> killsByShip = repo.SumPilotKillsByShip(kills, pilotID);
            ViewData["killSummary"] = killsByShip;

            

            return View(kills);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult PostComment(FormCollection fc)
        {
            KillRepository repo = new KillRepository();

            //TODO: check the values and make sure required values are sent.

            int killID = Convert.ToInt32(fc["killID"]);

            if (fc["commentText"].Trim().Length == 0)
            {
                ModelState.AddModelError("commentText", "Comment cannot be empty.");
            }

            if (fc["posterName"].Trim().Length == 0)
            {
                ModelState.AddModelError("posterName", "You have to tell us who you are.");
            }

            if (fc["postingPass"].Trim().Length == 0)
            {
                ModelState.AddModelError("postingPass", "This killboard requires a password to post comments.");
            }

            if (!ModelState.IsValid)
            {
                ViewData.Add("commentText", fc["commentText"]);
                ViewData.Add("posterName", fc["posterName"]);

            }
            else
            {

                try
                {
                    repo.AddComment(Convert.ToInt32(fc["killID"]), fc["commentText"], fc["posterName"], fc["postingPass"]);
                }
                catch (BadPasswordException bp)
                {
                    ModelState.AddModelError("postingPass", "Invalid password.");
                }
                catch (Exception e)
                {
                    ModelState.AddModelError(null, string.Format("An unexpected error occurred: {0}", e.ToString()));
                }
            }

            return View("KillDisplay2", repo.GetKill(killID));

        }

    }
}
